using System;
using System.Collections.Generic;
using System.Text;
using slcl.Packets;
using SimpleMessageQueue;

namespace slcl
{
    public class GroupsManager
    {

        Client Client;
        Grid Grid;
        MessageQueue MQueue;
        public GroupsManager(Client client)
        {
            Client = client;
            Grid = Client.Grid;
            MQueue = Client.MQueue;

            // Group callbacks
         //   MQueue.Send(new CommandMessage(Grid.Command.Register, PacketType.JoinGroupReply, new CommandMessage.AckCallback(JoinGroupHandler)), Grid);
         //   MQueue.Send(new CommandMessage(Grid.Command.Register, PacketType.LeaveGroupReply, new CommandMessage.AckCallback(LeaveGroupHandler)), Grid);
         //   MQueue.Send(new CommandMessage(Grid.Command.Register, PacketType.AgentDropGroup, new CommandMessage.AckCallback(ropGroupHandler)), Grid);
            
            //Agent Update Callback, it's our active group man!
        //    MQueue.Send(new CommandMessage(Grid.Command.Register, PacketType.AgentDataUpdate, new CommandMessage.AckCallback(AgentDataUpdateHandler)), Grid);


        }

        #region Commands

        /// <summary>
        /// Request to join a group. If there is an enrollment fee it will 
        /// automatically be deducted from your balance
        /// </summary>
        /// <param name="groupID">The group to attempt to join</param>
        public void RequestJoinGroup(LLUUID groupID)
        {
            JoinGroupRequestPacket join = new JoinGroupRequestPacket();

            join.AgentData.AgentID = Grid.AgentID;
            join.AgentData.SessionID = Grid.SessionID;
            join.GroupData.GroupID = groupID;

            Grid.SendPacket(join);
        }

        /// <summary>
        /// Request to leave a group
        /// </summary>
        /// <param name="groupID">The group to attempt to leave</param>
        public void RequestLeaveGroup(LLUUID groupID)
        {
            LeaveGroupRequestPacket leave = new LeaveGroupRequestPacket();

            leave.AgentData.AgentID = Grid.AgentID;
            leave.AgentData.SessionID = Grid.SessionID;
            leave.GroupData.GroupID = groupID;

            Grid.SendPacket(leave);
        }

        /// <summary>
        /// Set our current active group
        /// </summary>
        /// <param name="groupID">The group we are a member of that we want to 
        /// activate</param>
        public void ActivateGroup(LLUUID groupID)
        {
            ActivateGroupPacket activate = new ActivateGroupPacket();

            activate.AgentData.AgentID = Grid.AgentID;
            activate.AgentData.SessionID = Grid.SessionID;
            activate.AgentData.GroupID = groupID;

            Grid.SendPacket(activate);
        }


        #endregion


        #region Packet Handlers
        private void JoinGroupHandler(Packet packet, Simulator simulator)
        {
        //    if (OnJoinGroup != null)
        //    {
        //        JoinGroupReplyPacket reply = (JoinGroupReplyPacket)packet;

        //        OnJoinGroup(reply.GroupData.GroupID, reply.GroupData.Success);
        //    }
        }

        private void LeaveGroupHandler(Packet packet, Simulator simulator)
        {
       //     if (OnLeaveGroup != null)
        //    {
         //       LeaveGroupReplyPacket reply = (LeaveGroupReplyPacket)packet;

         //       OnLeaveGroup(reply.GroupData.GroupID, reply.GroupData.Success);
         //   }
        }

        public void AgentDataUpdateHandler(Packet packet, Simulator simulator)
        {
        //    AgentDataUpdatePacket p = (AgentDataUpdatePacket)packet;
        //    if (p.AgentData.AgentID == simulator.Client.Grid.AgentID)
        //    {
         //       activeGroup = p.AgentData.ActiveGroupID;
         //   }
        }

        private void DropGroupHandler(Packet packet, Simulator simulator)
        {
        //    if (OnGroupDropped != null)
        //    {
        //        OnGroupDropped(((AgentDropGroupPacket)packet).AgentData.GroupID);
        //    }
        }

        #endregion
    }
}
